package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.*;
import com.itheima.mapper.EmpExprMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.service.EmpLogService;
import com.itheima.service.EmpService;
import com.itheima.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description EmpServiceImpl
 * @Author 你的名字拼接或昵称
 * @Date 2025-09-03
 */
@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

    @Autowired
    private EmpExprMapper empExprMapper;

    @Autowired
    private EmpLogService empLogService;

    @Override
    public PageBean page(EmpQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());

        List<Emp> empList=empMapper.list(param);
        Page p=(Page) empList;
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Transactional
    @Override
    public void save(Emp emp) {
        try {
            emp.setPassword("123456");
            emp.setCreateTime(LocalDateTime.now());
            emp.setUpdateTime(LocalDateTime.now());
            empMapper.insert(emp);

            Integer id = emp.getId();

            List<EmpExpr> exprList=emp.getExprList();
            if(!CollectionUtils.isEmpty(exprList)){
                exprList.forEach((expr)->{
                    expr.setEmpId(id);
                });
                empExprMapper.insertBatch(exprList);
            }
        } finally {
            EmpLog empLog = new EmpLog();
            empLog.setOperateTime(LocalDateTime.now());
            empLog.setInfo("员工信息"+emp);
            empLogService.insertLog(empLog);
        }
    }

    @Transactional
    public void delete(List<Integer> ids) {
        //1.删除员工表信息
        empMapper.deleteBatch(ids);

        //  2.删除经历信息
        empExprMapper.deleteBatch(ids);
    }

    @Override
    public Emp getByBId(Integer id) {
//       1.查询员工基本信息,封装到emp里
        Emp emp = empMapper.getById(id);
//       2.查询员工经历信息,封装到emp里
        List<EmpExpr> empExpr =empExprMapper.getByEmpId(id);
        emp.setExprList(empExpr);
//        3.返回员工emp对象
        return emp;
    }

    @Transactional
    public void update(Emp emp) {
//        1.修改员工的基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.update(emp);
//        2.修改员工的工作经历(先删后增)
        empExprMapper.deleteByEmpId(emp.getId());
        List<EmpExpr> exprList=emp.getExprList();

        if(!CollectionUtils.isEmpty(exprList)){
//            关联员工id
            exprList.forEach(expr -> {
                expr.setEmpId(emp.getId());
            });
            empExprMapper.insertBatch(exprList);
        }

    }

    @Override
    public List<Emp> getList() {
        return empMapper.getlist();
    }

    @Override
    public LoginVo login(Emp emp) {
        Emp emplogin=empMapper.getUsernameAndPassword(emp);
        if (emplogin !=null){
            Map<String,Object> claims=new HashMap<>();
            claims.put("id",emplogin.getId());
            claims.put("username",emplogin.getUsername());

            String jwt = JwtUtils.generateJwt(claims);

            return new  LoginVo(emplogin.getId(),emplogin.getUsername(),emplogin.getName(),jwt);
        }

        return null;
    }

}
